using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using GestaoEventos.Interface;

namespace GestaoEventos.DataAccess
{
      public class D_AlimentosBebidas
      {
          #region Atributos

          private SqlConnection con;

          #endregion

          #region Construtores

          public D_AlimentosBebidas()
          {
              con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString());
          }

          #endregion

          #region Métodos Publicos

          public int Insert(I_AlimentosBebidas IAlimentosBebidas)
          {
              try
              {
                  string sql = "SPI_AlimentosBebidas";
                  SqlCommand cmd = new SqlCommand(sql);

                  cmd.Parameters.Add(new SqlParameter("@pNome", IAlimentosBebidas.Nome));

                  SqlParameter param = cmd.Parameters.Add("@pReturn", SqlDbType.Int);
                  param.Direction = ParameterDirection.Output;

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.ExecuteNonQuery();
                  con.Close();

                  return Convert.ToInt32(param.Value);
              }
              catch(Exception e)
              {
                  throw e;
              }
          }
          public int Update(I_AlimentosBebidas IAlimentosBebidas)
          {
              try
              {
                  string sql = "SPU_AlimentosBebidas";
                  SqlCommand cmd = new SqlCommand(sql);

                  cmd.Parameters.Add(new SqlParameter("@pId", IAlimentosBebidas.Id));
                  cmd.Parameters.Add(new SqlParameter("@pNome", IAlimentosBebidas.Nome));

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;
                  int RecordsAffected = cmd.ExecuteNonQuery();
                  con.Close();

                  return RecordsAffected;
              }
              catch(Exception e)
              {
                  throw e;
              }
          }
          public int Delete(I_AlimentosBebidas IAlimentosBebidas)
          {
              try
              {
                  string sql = "SPD_ALIMENTOSBEBIDAS";
                  SqlCommand cmd = new SqlCommand(sql);

                  cmd.Parameters.Add(new SqlParameter("@pId", IAlimentosBebidas.Id));

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;
                  int RecordsAffected = cmd.ExecuteNonQuery();
                  con.Close();

                  return RecordsAffected;
              }
              catch(Exception e)
              {
                  throw e;
              }
          }
          public ArrayList Select(I_AlimentosBebidas IAlimentosBebidas)
          {
              try
              {
                  string sql = "SPS_AlimentosBebidas";
                  SqlCommand cmd = new SqlCommand(sql);

                  if (IAlimentosBebidas.Id != 0)
                      cmd.Parameters.Add(new SqlParameter("@pId", IAlimentosBebidas.Id));
                  cmd.Parameters.Add(new SqlParameter("@pNome", IAlimentosBebidas.Nome));

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;

                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataTable dt = new DataTable();
                  da.Fill(dt);

                  con.Close();

                  ArrayList al = new ArrayList();
                  I_AlimentosBebidas IObj;
                  foreach(DataRow dr in dt.Rows)
                  {
                      IObj = new I_AlimentosBebidas();
                      IObj.Id = Convert.ToInt32(dr["Id"]);
                      IObj.Nome = dr["Nome"].ToString();

                      al.Add(IObj);
                  }

                  return al;
              }
              catch(Exception e)
              {
                  throw e;
              }
          }

          #endregion
      }
}

